<template>
  <div>
    <!-- 导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>数据参考</el-breadcrumb-item>
      <el-breadcrumb-item>期货合约介绍</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="25">
        <el-col :span="10">
          <el-input
            placeholder="请输入产品名称/合约代码"
            v-model="queryInfo.query"
            clearable
            @clear="getList"
          >
            <template #append>
              <el-button icon="el-icon-search" @click="getList"></el-button>
            </template>
          </el-input>
        </el-col>
      </el-row>
      <!-- 合约表格 -->
      <el-table :data="displayList" border stripe style="width: 100%;" :scroll-x="true">
        <el-table-column type="index" label="序号" width="60"></el-table-column>
        <el-table-column
          v-for="col in columns"
          :key="col"
          :label="col"
          :prop="col"
          :min-width="120"
          show-overflow-tooltip
        ></el-table-column>
      </el-table>
      <!-- 分页 -->
      <div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import csvData from '@/mock/FutureDataReferenceData.json'; // 你需要将csv转为json放到mock目录下

export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 10
      },
      allList: [],
      total: 0,
      columns: []
    }
  },
  computed: {
    displayList() {
      const start = (this.queryInfo.pageNum - 1) * this.queryInfo.pageSize;
      return this.filteredList.slice(start, start + this.queryInfo.pageSize);
    },
    filteredList() {
      if (!this.queryInfo.query) return this.allList;
      const q = this.queryInfo.query.trim().toLowerCase();
      return this.allList.filter(item =>
        (item['产品名称'] && String(item['产品名称']).toLowerCase().includes(q)) ||
        (item['合约代码'] && String(item['合约代码']).toLowerCase().includes(q))
      );
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.allList = csvData.filter(item => Object.keys(item).length > 1); // 过滤掉只有一个key的无效行
      this.total = this.filteredList.length;
      // 动态获取所有列名
      if (this.allList.length > 0) {
        this.columns = Object.keys(this.allList[0]);
      }
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.queryInfo.pageNum = 1;
      this.getList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getList();
    }
  }
}
</script>

<style scoped>
.el-table {
  overflow-x: auto;
}
</style>
